package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.shopping.CartAddForm;
import com.cskaoyan.gateway.form.shopping.CartNumUpdateForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/shopping")
public class CartController {

    // @Reference(timeout = 3000, check = false)
    @Reference(check = false)
    ICartService cartService;

    @GetMapping("/carts")
    public ResponseData cartList(HttpServletRequest servletRequest) {
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);

//        log.error("CartController中获取userInfo={}", userInfo);
         Long uid = Long.parseLong(object.get("uid").toString());

        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(uid);

        CartListByIdResponse response = cartService.getCartListById(cartListByIdRequest);

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getCartProductDtos());
            // return new ResponseUtil().setData(response);
        }

        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @PostMapping("/carts")
    public ResponseData cartAdd(@RequestBody CartAddForm form) {
        AddCartRequest addCartRequest = new AddCartRequest();
        addCartRequest.setUserId(form.getUserId());
        addCartRequest.setItemId(form.getProductId());
        addCartRequest.setNum(form.getProductNum());

        AddCartResponse response = cartService.addToCart(addCartRequest);

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }

        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @PutMapping("/carts")
    public ResponseData cartNumUpdate(@RequestBody CartNumUpdateForm form) {
        UpdateCartNumRequest updateCartNumRequest = new UpdateCartNumRequest();
        updateCartNumRequest.setUserId(form.getUserId());
        updateCartNumRequest.setItemId(form.getProductId());
        updateCartNumRequest.setNum(form.getProductNum());
        updateCartNumRequest.setChecked(form.getChecked());

        UpdateCartNumResponse response = cartService.updateCartNum(updateCartNumRequest);

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }

        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("/carts/{uid}/{pid}")
    public ResponseData cartItemDelete(@PathVariable("uid") Long uid, @PathVariable("pid") Long pid){
        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setUserId(uid);
        deleteCartItemRequest.setItemId(pid);

        DeleteCartItemResponse response = cartService.deleteCartItem(deleteCartItemRequest);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }

        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("/items/{id}")
    public ResponseData cartCheckedItemDelete(@PathVariable("id")Long uid){
        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
        deleteCheckedItemRequest.setUserId(uid);

        DeleteCheckedItemResposne response = cartService.deleteCheckedItem(deleteCheckedItemRequest);

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }

        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

}
